; Currently this code isn't being used.
; I cannot figure out if there's a way to
; get the system bank the BASIC ROM back
; into view when starting from a EasyFlash
; cartridge.  However, EasyLoader does it
; somehow and maybe after disecting that I'll
; figure it out.	
	
	.fopt		compiler,"cc65 v 2.13.9"
	.setcpu		"6502"
	.smart		on
	.autoimport	on
	.case		on
	.debuginfo	off
	.importzp	sp, sreg, regsave, regbank
	.importzp	tmp1, tmp2, tmp3, tmp4, ptr1, ptr2, ptr3, ptr4
	.macpack	longbranch
	.export		_execProgram
	
.segment "RUNNER"

.proc _execProgram
	jsr     pusha

	ldy     #$05	
	ldx     #$00
	lda     (sp),y	; Gets device parameter to A
	tax				; Put it in X

	lda #$01		; LFN
	ldy #$00		; Secondary Address
	jsr $FFBA		; SETLFS

	ldy #$02
	ldx #$00
	lda (sp),y		; Gets nameSize parameter to A
	pha

	ldy #$04
	ldx #$00
	lda (sp),y		; Gets nameHigh parameter to A
	pha

	ldy #$03
	ldx #$00
	lda (sp),y		; Gets nameLow parameter to A
	tax				; X has nameLow
	pla
	tay				; Y has nameHigh
	pla				; A has nameSize

	jsr $FFBD		; SETNAM

	ldy #$01
	ldx #$00
	lda (sp),y		; Gets loadLow parameter to A
	pha

	ldy #$00
	ldx #$00
	lda (sp),y		; Gets loadHigh parameter to A
	tay				; Y has load low
	sty vector + 1
	pla
	tax				; X has load high
	stx vector
	lda #$00

	stx $2b
	sty $2c

	jsr $FFD5		; LOAD

	stx $2d
	sty $2e
	
	; Now we have our file in memory, we need to
	; figure out how to launch it.
	lda vector + 1	; Gets loadHigh parameter to A
	sta $fb
	cmp #$08
	bne mlfound
	lda vector		; Gets loadLow parameter to A
	sta $fa
	cmp #$01
	bne mlfound2

	; BASIC program found.
	lda #36
	sta $01

	lda #$00
	sta $0800

	jsr $FF8A            ; RESTOR - Restore vectors to initial values
	jsr $FF81            ; CINT   - Initialize screen editor
	jsr $FF84            ; IOINIT - Initialize I/O devices

	jsr $a659 
	jsr $a533
	jmp $a7ae

	jmp done

mlfound2:
mlfound:
	ldy #$00
	lda ($fa),y

	cmp #$4c
	beq jumpNow

	lda #$37
	sta $01

	jmp (vector)

vector: .word $0000
	
jumpNow:
	lda #$37
	sta $01

	jmp ($fa)	

done:
	jsr popa;
	jsr popa;
	jsr popa;
	jsr popa;
	rts
.endproc